﻿//公共方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SN.Data;
using System.Data;
using SN.Models;
using System.Reflection;
namespace SN.Services
{
    public class SNService
    {

        public static string GetAppTitle(Assembly asm)
        {
           
            //如果是其他文件
            AssemblyTitleAttribute asmtitle = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyTitleAttribute));
            AssemblyCompanyAttribute asmcompn = (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(asm, typeof(AssemblyCompanyAttribute));
            string s = string.Format("{0}---{1}", asmtitle.Title, asmcompn.Company);
            return s;
        }

        public static string GetConnectServer()
        {
            string sdataBase = string.Empty;
            try
            {
                DbSession db = DBUtility.DB;
                using (System.Data.Common.DbConnection dbc = db.CreateConnection())
                {
                    sdataBase = string.Format("当前连接数据库为：{0} 数据状态为:{1} 数据库服务器{2}", dbc.Database, "已连接", dbc.DataSource);
                }
            }
            catch (Exception ex)
            {
                sdataBase = "数据库连接失败!";
            }
            return sdataBase;
        }

        public static string GetLSBH<T>() where T : SN.Data.Entity
        {
            string Msg = string.Empty;
            Type t = typeof(T);
            string tableName = t.FullName.Substring(t.FullName.LastIndexOf('.') + 1); //取实体表名
            r_Config pConf = null;
            pConf = DBUtility.DB.Single<r_Config>(r_Config._.Config_Key == tableName);
            if (pConf == null)
            {
                pConf = new r_Config();

                pConf.Config_Key = tableName;
                pConf.Config_Value = "1";
                pConf.Config_Note = tableName;

                pConf.Detach();
            }
            else
            {
                pConf.Config_Value = Convert.ToString(Convert.ToInt16(pConf.Config_Value) + 1);
                pConf.Attach();
            }
            DBUtility.DB.Save<r_Config>(pConf);
            return pConf.Config_Value.PadLeft(6, '0');//长度为6位，能满足需求
        }

        #region Show Error
        public static void ShowError(string msg)
        {
            MessageBox.Show(msg);
        }
        #endregion

        #region Hourglass
        public static void Hourglass(bool Show)
        {
            if (Show == true)
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            }
            else
            {
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            }
            return;
        }
        #endregion

    }



}
